package com.lsm.test;

import com.lsm.entity.Product;
import com.lsm.mapper.ProductMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.SortedMap;


public class MybatisProductTest3 {

    SqlSession sqlSession;
    ProductMapper productMapper;
    @Before
    public void beforeTest() throws IOException {
        //加载核心配置文件SqlMapConfig.xml
        InputStream is=Resources.getResourceAsStream("SqlMapConfig.xml");
        //获取SqlSession工厂对象
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(is);
        //获取SqlSession对象
        sqlSession=sqlSessionFactory.openSession(true);
        productMapper =sqlSession.getMapper(ProductMapper.class);
    }


    //新增
    @Test
    public void testAddProduct() throws IOException {

        //创建Product对象
        Product p=new Product();
        p.setProductNum("9529");
        p.setProductName("太湖三节课游9529");
        p.setCityName("太湖9529");
        p.setDepartureTime(new Date());
        p.setProductPrice(1000.0);
        p.setProductDesc("无锡太湖学院真美丽9529");
        p.setProductStatus(1);
        //执行插入，把Product插入数据库表product中
        //sqlSession.insert("productMapper.addProduct",p);
        //此时不需要手动提交事务，会自动提交事务
        ProductMapper productMapper =sqlSession.getMapper(ProductMapper.class);
        int a=productMapper.addProduct(p);
        System.out.println(a);
    }


    @Test
    public void testQueryProductAll() throws IOException{
        List<Product> list =productMapper.queryProductAll();
        list.stream().forEach(System.out::println);
    }


    @Test
    public void testQueryProductById() throws IOException{
        Product product=productMapper.queryProductById(12);
        System.out.println(product);
    }

    @Test
    public void testUpdateProductById() throws IOException{
        Product product=productMapper.queryProductById(1);
        product.setProductName("13132");
        product.setCityName("vuvyu");
        int a = productMapper.updateProductById(product);
        System.out.println(a);
    }

    @Test
    public void testDeleteProductById() throws IOException{
        int a=productMapper.deleteProductById(15);
        System.out.println(a);
    }
    @Test
    public void testDeleteProductByBatch() throws IOException{
        List<Integer> ids=new LinkedList<>();
        ids.add(11);
        ids.add(12);
        ids.add(14);
        int a=productMapper.deleteProductByBatch(ids);
        System.out.println(a);
    }



    @Test
    public void queryProductList() throws IOException{
        //获取查询
        List<Product> list= sqlSession.selectList("queryProductAll");
        //System.out.println(list);
        //遍历集合
        for(Product product:list){
            System.out.println(product);
        }
    }
    @After
    public void afterTest(){
        //释放资源
        sqlSession.close();
    }
}
